package com.yao.sdk.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.yao.sdk.R;

import java.io.File;



/**
 * Created by Administrator on 2016/7/14.
 */
public class GlideUtil {
    public static final int NORMAL=0X001, VERTICAL =0X002,HROIZONTAL=0X003;
    private static GlideUtil mGlideUtil;
    private int placeHolderImg = R.drawable.error_img;
    private int placeHolderVerImg = R.drawable.error_img_ver;
    private int placeHolderHorImg = R.drawable.error_img_hor;
    private int placeHolderBankImg = R.drawable.error_img_square;
    private int placeHolderAvaImg = R.drawable.def_avatar;
    private int placeHolderGroupImg = R.drawable.group_def;
    private int errorVerImg = R.drawable.error_img_ver;
    private int errorHorImg = R.drawable.error_img_hor;
    private int errorSquImg = R.drawable.error_img;
    private int errorImg = R.drawable.error_img;
    private int errorBankImg = R.drawable.error_img_square;
    private int errorAvatarImg = R.drawable.def_avatar;

    private GlideUtil() {
    }

    public static GlideUtil getInstance() {
        if (mGlideUtil == null) {
            mGlideUtil = new GlideUtil();
        }
        return mGlideUtil;
    }

    public void setPlaceHolder(int resourceId) {
        this.placeHolderImg = resourceId;
    }

    public void setPlaceHolderAvatar(int resourceId) {
        this.placeHolderAvaImg = resourceId;
    }

    public void setError(int resourceId) {
        this.errorImg = resourceId;
    }

    public void setErrorAvatar(int resourceId) {
        this.errorAvatarImg = resourceId;
    }
    private int getPlaceHolder(int type){
        switch (type){
            case NORMAL:
                return placeHolderImg;
            case VERTICAL:
                return placeHolderVerImg;
            case HROIZONTAL:
                return placeHolderHorImg;
            default:
                return placeHolderImg;
        }
    }
    private int getErrorImg(int type){
        switch (type){
            case NORMAL:
                return errorImg;
            case VERTICAL:
                return errorVerImg;
            case HROIZONTAL:
                return errorHorImg;
            default:
                return errorImg;
        }
    }
    public void load(Context context, String url, ImageView imageView,boolean centerCrop) {
        load(context,url,imageView,centerCrop,getPlaceHolder(NORMAL),getErrorImg(NORMAL));
    }
    public void load(Context context, String url, ImageView imageView,boolean centerCrop,int placeHolderType) {
        load(context,url,imageView,centerCrop,getPlaceHolder(placeHolderType),getErrorImg(placeHolderType));
    }
    public void load(Context context, String url, ImageView imageView, boolean centerCrop,int placeHolderImg,int errorImg) {
        if (centerCrop) {
            Glide.with(context).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(placeHolderImg).error(errorImg).centerCrop().into(imageView);
        } else {
            Glide.with(context).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(placeHolderImg).error(errorImg).into(imageView);
        }

    }
    public void loadGif(Context context, String url, ImageView imageView,boolean centerCrop){
        loadGif(context,url,imageView,centerCrop,getPlaceHolder(NORMAL),getErrorImg(NORMAL));
    }
    public void loadGif(Context context, String url, ImageView imageView,boolean centerCrop,int placeHolderType){
        loadGif(context,url,imageView,centerCrop,getPlaceHolder(placeHolderType),getErrorImg(placeHolderType));
    }

    public void loadGif(Context context, String url, ImageView imageView, boolean centerCrop,int placeHolderImg,int errorImg) {
        if (url != null && (url.endsWith(".gif") || url.endsWith(".GIF"))) {
            if (centerCrop) {
                Glide.with(context).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().centerCrop().into(imageView);
            } else {
                Glide.with(context).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().into(imageView);
            }

        } else {
            if (centerCrop) {
                Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().centerCrop().into(imageView);
            } else {
                Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().into(imageView);
            }

        }

    }
    
    public void loadRound(Context context, String url, ImageView imageView, boolean asGif, boolean centerCrop, int roundDp) {

        if (asGif) {
            if (url != null && (url.endsWith(".gif") || url.endsWith(".GIF"))) {
                if (centerCrop) {
                    Glide.with(context).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().centerCrop().into(imageView);
                } else {
                    Glide.with(context).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().into(imageView);
                }

            } else {
                if (centerCrop) {
                    Glide.with(context).load(url).transform(new GlideRoundTransform(context, roundDp)).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().centerCrop().into(imageView);
                } else {
                    Glide.with(context).load(url).transform(new GlideRoundTransform(context, roundDp)).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).dontAnimate().into(imageView);
                }

            }
        } else {
            if (centerCrop) {
                Glide.with(context).load(url).asBitmap().transform(new GlideRoundTransform(context, roundDp)).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).centerCrop().into(imageView);
            } else {
                Glide.with(context).load(url).asBitmap().transform(new GlideRoundTransform(context, roundDp)).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(errorImg).error(errorImg).into(imageView);
            }

        }

    }

    public void loadResource(Context context, int resourceId, ImageView imageView, boolean asGif) {
        if (asGif) {
            Glide.with(context).load(resourceId).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).centerCrop().into(imageView);

        } else {
            Glide.with(context).load(resourceId).diskCacheStrategy(DiskCacheStrategy.SOURCE).centerCrop().into(imageView);

        }
    }

    public void loadFile(Context context, File file, ImageView imageView, boolean asGif) {
        if (asGif) {
            if (file.getAbsolutePath().endsWith(".gif") || file.getAbsolutePath().endsWith(".GIF")) {
                Glide.with(context).load(file).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).centerCrop().into(imageView);
            } else {
                Glide.with(context).load(file).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).centerCrop().into(imageView);
            }
        } else {
            Glide.with(context).load(file).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).centerCrop().into(imageView);
        }


    }
    public void loadFile(Context context, File file, ImageView imageView, boolean asGif, boolean centerCrop) {
        if (asGif) {
            if (file.getAbsolutePath().endsWith(".gif") || file.getAbsolutePath().endsWith(".GIF")) {
                if (centerCrop){
                    Glide.with(context).load(file).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).centerCrop().into(imageView);

                }else {
                    Glide.with(context).load(file).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).into(imageView);

                }
            } else {
                if (centerCrop){
                    Glide.with(context).load(file).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).centerCrop().into(imageView);

                }else {
                    Glide.with(context).load(file).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).into(imageView);

                }
            }
        } else {
            if (centerCrop){
                Glide.with(context).load(file).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).centerCrop().into(imageView);

            }else {
                Glide.with(context).load(file).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).error(errorImg).into(imageView);

            }
        }


    }
//    public void load(final Context context, String url, final onBitMapRequestListener mListener) {
//        Glide.with(context).load(url).asBitmap()
//                .placeholder(placeHolderImg)
//                .centerCrop()
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)
//                .into(new SimpleTarget<Bitmap>() {
//                    @Override
//                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                        super.onLoadFailed(e, errorDrawable);
//                        if (mListener != null) {
//                            mListener.onFail(e);
//                        }
//                        Log.e("zwj","....onLoadFailed");
//
////                        Glide.with(context).loadFile(errorSquImg).into(imageView);
//                    }
//
//                    @Override
//                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
////                        imageView.setImageBitmap(resource);
//                        if (mListener != null) {
//                            mListener.onSuccess(resource);
//                        }
//                    }
//                });
//
//    }
//
//    public void load(final Context context, String url, final onFileRequestListener mListener) {
//        Glide.with(context)
//                .load(url)
//
//                .downloadOnly(new SimpleTarget<File>() {
//                    @Override
//                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                        super.onLoadFailed(e, errorDrawable);
//                        if (mListener!=null)
//                            mListener.onFail(e);
//                    }
//
//                    @Override
//                    public void onResourceReady(final File resource, GlideAnimation<? super File> glideAnimation) {
//                        if (mListener!=null)
//                            mListener.onSuccess(resource);
//
//                    }
//                });
//
//    }
//    public void load(final Context context, String url, final ImageView imageView, final onLoadFinishListener mListener) {
//        Glide.with(context).load(url)
//                .centerCrop()
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)
//                .into(new GlideDrawableImageViewTarget(imageView){
//                    @Override
//                    public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
//                        super.onResourceReady(drawable, anim);
//                        if (mListener!=null){
//                            mListener.onFinish();
//                        }
//                        //在这里添加一些图片加载完成的操作
//                    }
//                });
//                    ;
//
//    }
    public void loadVideo(Context context, File file, ImageView imageView) {
        Glide.with(context)
                .load(Uri.fromFile(file))
                .into(imageView);
    }

    public void loadAvatar(Context context, String url, ImageView imageView) {
        Glide.with(context).load(url).transform(new GlideCircleTransform(context)).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(placeHolderAvaImg).error(errorAvatarImg)
                .crossFade(300).into(imageView);
    }
    public void loadAvatar2(Context context, String url, ImageView imageView) {
        Glide.with(context).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(placeHolderAvaImg).error(errorAvatarImg).centerCrop().into(imageView);

    }





    public interface onBitMapRequestListener {
        void onFail(Exception e);

        void onSuccess(Bitmap map);
    }
    public interface onFileRequestListener {
        void onFail(Exception e);

        void onSuccess(File map);
    }
    public interface onLoadFinishListener {
        void onFinish();
        void onException();
    }

}
